package com.my.spvideo.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.my.spvideo.service.LoginService;
import com.my.spvideo.util.Verify;

/**
 * 登录
 * @author zhangcheng
 *
 */
@Controller
public class LoginController extends BasicController{
//	private final Logger logger = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private LoginService loginService;
	
	@RequestMapping("/login")
	public void login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		JSONObject json = new JSONObject();
		String username = request.getParameter("uname");
		String password = request.getParameter("pwd");
		if(username==null||!Verify.verifyUsername(username)){
			to405(request, response, "参数异常");
			return;
		}
		if(password==null||!Verify.verifyPassword(password)){
			to405(request, response, "参数异常");
			return;
		}
		if(loginService.login(request,username, password)){
			json.put("resc", "0000");
			writeJSON(response, json);
			return;
		}else{
			json.put("resc", "0001");
			writeJSON(response, json);
			return;
		}
	}
	
	@RequestMapping("/logout")
	public void logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
//		if(logger.isDebugEnabled()){
//			logger.debug("登出请求");
//		}
		request.getSession().invalidate();
	}
	
}
